var players = [[]];
var gameBoardOpen = false;
var loggedIn = false;
var gameOn = false;
var yourTurn;
var serverOkey = true;
// Button names
var instructionButtonName = "Instructions";
var highScoreButtonName = "High Score";
var startButtonName = "Start";
var newGameButtonName = "New Game";
var lobbyButtonName = "Go To Lobby";
var boardButtonName = "Game Board";
var replayButtonName = "View Replay";
var logInButtonName  = "Log In";
var logOutButtonName  = "Log Out";



function hideIfVisible(id){
	if ($("#" + id).is(":visible")){
		$("#" + id).hide("fast");
		return true;
	}	
	return false;
}

function tryToggle(id, buttonName){ 
	if ($("#" + id).is(":visible")){
		$("#" + id).hide("fast");
		if (!loggedIn){
			$("#login").delay(200).show("fast");
		}
		else if (!gameBoardOpen){
			$("#lobby").delay(200).show("fast");
		}
		else{
			$("#middlePart").delay(200).show("fast");
		}
		changeButtonNameTo(id + "Button", buttonName);
	}
}

function changePageTo(id, bool, buttonName){
	tryToggle(id, buttonName);
	
	if (bool){
		$('#'+id).delay(200).show("fast");
		changeButtonNameTo("lobbyButton", lobbyButtonName);
		if (!loggedIn){
			changeButtonNameTo(id + "Button", logInButtonName);
		}
		else if (!gameBoardOpen){
			changeButtonNameTo(id + "Button", lobbyButtonName);
		}
		else{
			changeButtonNameTo(id + "Button", boardButtonName);
		}
		return true;
	}
	return false;
}

function setToPlayingState(){
	hideIfVisible("instructions");
	hideIfVisible("highScore");
	hideIfVisible("lobby");
	hideIfVisible("instructions");
	hideIfVisible("replay");
	$("#lobbyButton").show("fast");
	$("#startButton").attr("onclick", "playGame()");
	changeButtonNameTo("lobbyButton", lobbyButtonName);
	changeButtonNameTo("startButton", startButtonName);
	changeButtonNameTo("instructionsButton", instructionButtonName);
	changeButtonNameTo("highScoreButton", highScoreButtonName);
	changeButtonNameTo("replayButton", replayButtonName);
	setTimeout("$('#middlePart').show('fast')",250);
}


function instructionsToggle() {
	$("#errorArea").hide("fast");
	var closed = hideIfVisible("middlePart") || hideIfVisible("lobby") || hideIfVisible("highScore") || hideIfVisible("login") || hideIfVisible("replay");
	if (changePageTo("instructions", closed, instructionButtonName)){
		changeButtonNameTo("highScoreButton", highScoreButtonName);
		changeButtonNameTo("replayButton", replayButtonName);
	}
}


function highScoreToggle() {
	$("#errorArea").hide("fast");
	var closed = hideIfVisible("middlePart") || hideIfVisible("lobby") || hideIfVisible("instructions") || hideIfVisible("login") || hideIfVisible("replay");
	if (changePageTo("highScore", closed, highScoreButtonName)){
		changeButtonNameTo("instructionsButton", instructionButtonName);
		changeButtonNameTo("replayButton", replayButtonName);
	};
}

function lobbyToggle(){
	$("#errorArea").hide("fast");
	var closed = hideIfVisible("middlePart") || hideIfVisible("instructions") || hideIfVisible("highScore") || hideIfVisible("replay");
	if (changePageTo("lobby", closed, lobbyButtonName)){
		changeButtonNameTo("instructionsButton", instructionButtonName);
		changeButtonNameTo("highScoreButton", highScoreButtonName);
		changeButtonNameTo("replayButton", replayButtonName);
	}
}

function replayToggle(){
	$("#errorArea").hide("fast");
	var closed = hideIfVisible("middlePart") || hideIfVisible("lobby") || hideIfVisible("instructions") || hideIfVisible("login") || hideIfVisible("highScore");
	if (changePageTo("replay", closed, replayButtonName)){
		changeButtonNameTo("instructionsButton", instructionButtonName);
		changeButtonNameTo("highScoreButton", highScoreButtonName);
		generateReplayList();
		$('#replayListTable tr').click( function() { 
	        replay($(this).attr('id')); 
	    }); 
	};
}

function changeButtonNameTo(id, newName) {
		document.getElementById(id).innerHTML = newName;
}


function newGame(){
	if (!loggedIn){
		$("#instructions").hide("fast");
		$("#highScore").hide("fast");
		$("#login").delay(200).show("fast");
		changeButtonNameTo("instructionsButton", instructionButtonName);
		changeButtonNameTo("highScoreButton", highScoreButtonName);
	}
	else{
		createNewGame();
	}
}


function playGame() {
	sendShipLocations();
	$("#opponentTable .shootable").click(function(){
    	shootIfYourTurn($(this).attr("id"));
});
	if (myBoard.gameAllowed()) {
		setToPlayingState();
		gameOn = true;
    	changeButtonNameTo("startButton", "Leave");
    	$("#startButton").attr("onclick", "leaveGame()");
    	shootingAllowed = true;
    	turningAllowed = false;
    	getGameId();
    	getP1Name();
		getP2Name();
        $("#errorArea").delay(1500).hide("fast");
        $("#playerTable td").droppable("disable");
        $(".bs").dblclick(function () {return false;});
    	checkShot();        
    }
    else {
    	$("#errorArea").show("fast");
        document.getElementById("errorArea").innerHTML = "Seems that you have some problems with warboats, deal with that!";
    }
}



function refresh(){
	location.reload(true);
}


